java 多线程

Posted by ZhengYang on 2016-09-17

Thread

  1. TestThread类 继承Thread类
  2. 重写run()方法
  3. new TestThread().start() 启动一个新的线程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class TestThread extends Thread{
public void run(){
while(true){
System.out.println(Thread.currentThread());
}
}
}
public class ThreadDemo1 {
public static void main(String[] args){
//多线程,启动TestThread.run()方法,不等TestThread.run()返回,就直接自己运行
new TestThread().start();
//单线程,直接调用TestThread.run()方法,必须等TestThread.run()返回,自己才能接着运行
new TestThread().run();
// 不会执行到这里
// 因为start开了一个线程Thread-0,去执行run方法了;
// 线程main会继续执行,然而下一句却是run()方法,所以也去执行run方法了
// 所以,接下来的语句不会执行
while(true){
System.out.println(Thread.currentThread());
}
}
}

Thread 匿名内部类实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ThreadDemo2 {
public static void main(String[] args) {
// 匿名内部类实现 Thread的子类+new
Thread t = new Thread(){
public void run(){
while(true)
System.out.println(Thread.currentThread());
}
};
t.start();
while(true)
System.out.println(Thread.currentThread());
}
}

Runnable

  1. TestThread类 实现Runnable接口
  2. 实现run方法
  3. TestThread t = new TestThread()
  4. new Thread(t).start() 启动一个新的线程 (比Thread方法多一步)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class TestRunnable implements Runnable{
public void run(){
while(true)
System.out.println(Thread.currentThread());
}
}
public class ThreadDemo3 {
public static void main(String[] args) {
TestRunnable tt = new TestRunnable();
Thread t = new Thread(tt);
t.start();
while(true)
System.out.println(Thread.currentThread());
}
}

Runnable 匿名内部类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Runnable 匿名内部类
public class ThreadDemo4 {
public static void main(String[] args) {
Runnable r = new Runnable(){
public void run(){
while(true)
System.out.println(Thread.currentThread());
}
};
new Thread(r).start();
while(true)
System.out.println(Thread.currentThread());
}
}

Thread 和 Runnable 区别

Thread
一个线程对象无论多少次start(),只能启动一次。
Runnable
适合多个相同程序代码的线程,去处理同一份资源
可以避免单继承的局限

例子

一个资源,两个线程做++操作,两个线程做–操作,且同步。

Thread 实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class ThreadDemo200 {
int size = 100;
public synchronized void increase(){
System.out.println(size++ + Thread.currentThread().getName());
}
public synchronized void decrease(){
System.out.println(size-- + Thread.currentThread().getName());
}
class T1 extends Thread{
public void run(){
while(true)
increase();
}
}
class T2 extends Thread{
public void run(){
while(true)
decrease();
}
}
public static void main(String[] args){
// 资源只要new一份就可以
ThreadDemo200 t = new ThreadDemo200();
t.new T1().start();
t.new T1().start();
t.new T2().start();
t.new T2().start();
}
}
Runnable 实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class ThreadDemo100 {
int size = 100;
public synchronized void increase(){
System.out.println(size++ + Thread.currentThread().getName());
}
public synchronized void decrease(){
System.out.println(size-- + Thread.currentThread().getName());
}
class R1 implements Runnable{
public void run(){
while(true)
increase();
}
}
class R2 implements Runnable{
public void run(){
while(true)
decrease();
}
}
public static void main(String[] args) {
// 只new了一次,所以资源只有一份
ThreadDemo100 t = new ThreadDemo100();
// 资源没有在内部类中,所以new了两个并不影响
Runnable r1 = t.new R1();
Runnable r2 = t.new R2();
new Thread(r1).start();
new Thread(r1).start();
new Thread(r2).start();
new Thread(r2).start();
}
}